package topcoder;

public class TheNumberGameDiv2 {

	String convert(int A) {
		StringBuilder s = new StringBuilder();
		while (A > 0) {
			int d = A % 10;
			A = A / 10;
			s.insert(0, String.valueOf(d));
		}
		return s.toString();
	}
	
	String reverse(String a) {
		StringBuilder s = new StringBuilder();
		for(int i = a.length() - 1; i >= 0; i--)
			s.append(a.charAt(i));
		return s.toString();
	}
	
	public int minimumMoves(int A, int B) {
		String S = convert(A);
		String R = reverse(S);
		String T = convert(B);
		int n = S.length();
		int m = T.length();
		
		int i, x, z;
		int c = Integer.MAX_VALUE;
		if((i = R.indexOf(T)) >= 0) {
			x = n - i - m;
			z = i;
			c = x + 1 + z;
		}
		if ((i = S.indexOf(T)) >= 0) {
			z = n - i - m;
			x = i;
			if (x > 0)
				c = Math.min(c, x + 2 + z);
			else
				c = Math.min(c, x + z);
		}

		if (c < Integer.MAX_VALUE)
			return c;
		else
			return -1;
	}
}
